package com.dsbj.mp.icardservice.controller;

import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.mp.icardservice.model.MpUser;
import com.dsbj.mp.icardservice.service.SerialNumberService;
import com.dsbj.mp.icardservice.service.WxUserService;
import com.dsbj.mp.icardservice.utils.Constants;
import com.dsbj.mp.icardservice.utils.jwt.JwtTokenUtil;
import com.dsbj.mp.icardservice.utils.strprocess.StrUtil;
import com.dsbj.mp.icardservice.webserviceclient.dsbjpmc.WxUser;
import com.dsbj.mp.icardservice.webserviceclient.dsbjpmc.WxUserResponseMessage;
import com.dsbj.starter.redis.utils.MultiRedisHelper;
import com.dsbj.starter.redis.utils.RedisHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户token相关接口。
 * 该类使用了redis缓存来存储用户的token信息。
 *
 * @author cjw 2019-11-26
 */
@RestController
@RequestMapping(value = "/token")
public class JwtTokenController extends BaseController {

	@Autowired
	private WxUserService wxUserService;

	@Autowired
	private JwtTokenUtil jwtTokenUtil;

	@Autowired
	private SerialNumberService serialNumberService;

	@Autowired
	private RedisHelper redisHelper;

	@Autowired
	private MultiRedisHelper multiRedisHelper;


	/**
	 * 根据openId获取token令牌。
	 * 用户token由USER_ACCESS_TOKEN, UUID, employeeno组成，格式为《USER_ACCESS_TOKEN_45fd9323733f4671832ab2605796957e_D501900993》。
	 *
	 * @param
	 * @return
	 * @author cjw 2019-11-26
	 */
	@RequestMapping(value = "getTokenByOpenId")
	public ResponseMessage getTokenByOpenId(String openId){
		//
		if(StrUtil.isEmpty(openId)){
			return ResponseMessage.error("未传入有效的openId!");
		}
		//根据openId获取wxUser信息
		ResponseMessage wxUserResponseMessage=wxUserService.getMpUserByOpenId(openId);
		if(!wxUserResponseMessage.isSuccess()||wxUserResponseMessage.getData()==null){
			return ResponseMessage.error("未找到openId对应的用户信息!");
		}

		/**
		 * 根据openId等信息生成token令牌
		 */
		MpUser wxUser= (MpUser) wxUserResponseMessage.getData();
		String token = null;
		try {
			//token = jwtTokenUtil.generateToken(new JWTInfo(openId,wxUser.getEmployeeNo(),wxUser.getName()));
			//redisHelper.set(token,wxUser, Constants.EXPIRE_USER_TOKEN);

			token = SerialNumberService.generateUserToken(wxUser.getEmployeeNo());
			multiRedisHelper.set(token, wxUser, Constants.EXPIRE_USER_TOKEN, Constants.REDISDB_ACCESS_TOKEN);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("根据openId获取token令牌失败!"+e.toString());
			return ResponseMessage.error(e.toString());
		}
		logger.info("[openId="+openId+"]对应的token为: "+token);
		return ResponseMessage.ok(token);
	}


	/**
	 * 根据员工工号删除其对应的token信息。
	 * @param employeeNo
	 * @return
	 */
	@RequestMapping(value = "removeToken")
	public ResponseMessage removeToken(String employeeNo){
		//
		if(StrUtil.isEmpty(employeeNo)){
			return ResponseMessage.error("工号不能为空!");
		}
		multiRedisHelper.removeByKeyPattern(String.format("%s_*_%s",Constants.USER_TOKEN_PREFIX,employeeNo),Constants.REDISDB_ACCESS_TOKEN);
		return ResponseMessage.ok();
	}



}
